<?php

function get_template($typetpl, $params = null) {
    $html = null;
    switch ($typetpl) {
        case 1:
            $html = get_temp_frm_add_new();
            break;
        case 2:
            $html = get_temp_list_category();
            break;
        case 3:
            if ($params != null && is_array($params)) {
                if (is_numeric($params['catId'])) {
                    $html = get_temp_update_category($params['catId']);
                }
            }
            break;
        case 4:
            $html = get_list_news_by_category();
            break;
        case 5:
            $html = get_all_post();
            break;
        case 6:
            $html = get_edit_post_template($params);
            break;
        case 7:
            $html = get_view_content_post($params);
            break;
        case 8:
//            $html = get_list_new_by_category();
            if ($params != null && is_array($params)) {
                if (is_numeric($params['catId'])) {
                    $html = get_list_new_by_category($params);
                }
            }
            break;
        default:
            break;
    }
    return $html;
}

/*
 *  Get template of frm add new
 */

function get_temp_frm_add_new() {
    global $tpl, $az_db;
    $file_tpl = 'news/frm_add_new';
    $html = $tpl->get_main($file_tpl, 2);
    $catlist = get_temp_category();
    $html = $tpl->get_vars($html, array('add_new_list_categories' => $catlist, 'action' => 'action.php?act=1'));
    return $html;
}

/*
 * Get list news by category
 */

function get_list_news_by_category() {
    
}

/*
 * Function get all post
 */

function get_all_post() {
    global $tpl, $az_db;
    //Create list user
    $pager = new Az_Pager();
    $file_tpl = 'news/list_new';
    $listnew = $tpl->get_main($file_tpl, 2);

    //Create a params is array to get a block in list_user.html file
    $list_new['list_new'] = $tpl->get_block($listnew, 'list_new', 1);
    //Create a $sql query for get all total category 
    $sql = "SELECT COUNT(*) FROM az_news";
    $result = $az_db->executeSQL_Query($sql)->fetch_row();
    $totalRecord = $result[0];


    //Create params for pager
    $scroll_page = 5;
    $per_page = 6;
    $current_page = isset($_GET['page']) ? $_GET['page'] : 1;
    $url = 'index.php?page=';

    //Setup a page
    $pager->page_set($url, $totalRecord, $scroll_page, $per_page, $current_page);

    //Create $sql query for get record of a page
    $sql_P = "SELECT * FROM az_news ORDER BY news_id DESC LIMIT " . $pager->start . "," . $pager->per_page;
    $result_P = $az_db->executeSQL_Query($sql_P);
    if ($result_P->num_rows > 0) {
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            $newId = $rows['news_id'];
            $title = $rows['title'];
            $author = $rows['author'];
            $descr = $rows['description'];
            $content = $rows['content'];
            $category = getCategoryNameByString($rows['category_id']);
            $active = $rows['status'];
            $date = $rows['date_edit'];
            $comment = 1; //$rows['email'];
            $checkbox = '<input type="checkbox" name="newsId[]" value="' . $newId . '"/>';
            $actions = '<a href="actionform.php " title="Add" class="icon-add info-tooltip"></a>
                        <a href="edit.php?newId=' . $newId . ' " title="Edit" class="icon-edit info-tooltip"></a>
                        <a href="action.php?act=3&newId=' . $newId . ' " title="Delete" class="icon-delete info-tooltip" onclick="return deletechecked();"></a>
                        <a href="action.php?act=4&newId=' . $newId . ' " title="Active" class="icon-active info-tooltip"></a>
                        <a href="view.php?newId=' . $newId . ' " title="View" class="icon-view info-tooltip"></a>
                        <a href="action.php?act=6&newId=' . $newId . ' " title="Set hot" class="icon-hot info-tooltip"></a>';
            $html .= $tpl->get_vars($list_new['list_new'], array('check_box' => $checkbox, 'title' => $title, 'author' => $author,
                        'categories' => $category, 'comment' => $comment, 'date' => $date, 'active' => $active, 'action' => $actions));
        }
        $result_P->free();
        //Assign $html
        $html = $tpl->assign_blocks_content($listnew, array('list_new' => $html));
        $html = $tpl->get_vars($html, array('page_par_left' => $pager->firt_page,
                    'page_left' => $pager->previous_page, 'page-info' => $pager->page_info,
                    'page-right' => $pager->next_page, 'page-far-right' => $pager->last_page));
        return $html;
    } else {
        $result_P->free();
        $html = "<center><i>Không có người sử dụng nào trong hệ thống</i></center>";
        return $html;
    }
}

/*
 *  function get all category name by string category name;
 */

function getCategoryNameByString($category) {
    if (is_string($category) && strlen($category) > 0) {
        $strcatNameLink = '';
        $arrayCat = explode(",", $category);
        foreach ($arrayCat as $value) {
            $strcatNameLink .= exequeryGetCategoryName($value);
        }
        return $strcatNameLink;
    } else {
        return '';
    }
}

/*
 *  function get category sql; 
 */

function exequeryGetCategoryName($catId) {
    global $az_db;
    $sql = "SELECT `name` , `category_id` FROM  `az_category` WHERE `category_id`=" . $catId;
    $result_P = $az_db->executeSQL_Query($sql);
    $html = '';
    if ($result_P->num_rows > 0) {
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            return '<a href="litscatnew.php?catId=' . $rows['category_id'] . '&name=' . $rows['name'] . '">' . $rows['name'] . '</a>';
        }
        return '';
    } else {
        return '';
    }
}

/*
 * Function get category for add new
 */

function get_temp_category() {
    global $az_db;
    $sql = "SELECT  `category_id` ,`name` , `category_parent_id` FROM  `az_category` WHERE `category_parent_id` = 0 ORDER BY `name` ASC ";
    $result_P = $az_db->executeSQL_Query($sql);
    $html = '';
    if ($result_P->num_rows > 0) {
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            $html.=' <li><input type="checkbox" name="categories[]" value="' . $rows['category_id'] . '"/>&nbsp;' . $rows['name'] . '</li>';
            $html.= get_sub_category($rows['category_id'], '&nbsp;&nbsp;');
        }
        $result_P->free();
    } else {
        $result_P->free();
        return null;
    }
    return $html;
}

/*
 * Get subcategory
 */

function get_sub_category($cat_id, $str) {
    global $az_db;
    $sql = "SELECT  `category_id` ,`name` , `category_parent_id` FROM  `az_category` WHERE category_parent_id = " . $cat_id;
    $result_P = $az_db->executeSQL_Query($sql);
    if ($result_P->num_rows > 0) {
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            $html.='<li><input type="checkbox" name="categories[]" value="' . $rows['category_id'] . '"/>&nbsp;' . $str . $rows['name'] . '</li>';
            $html.=get_sub_category($rows['category_id'], '&nbsp;&nbsp;&nbsp;&nbsp;');
        }
        $result_P->free();
    } else {
        $result_P->free();
        return null;
    }
    return $html;
}

/*
 * Get list category 
 */

function get_list_category_image($category) {
    if ($category != '' && is_string($category)) {
        $catarr = explode(',', $category);
        return get_temp_category_foredit_new($catarr);
    }
}

/*
 * Function get category for add new
 */

function get_temp_category_foredit_new($catarr) {
    global $az_db;
    $sql = "SELECT  `category_id` ,`name` , `category_parent_id` FROM  `az_category` WHERE `category_parent_id` = 0 ORDER BY `name` ASC ";
    $result_P = $az_db->executeSQL_Query($sql);
    $html = '';
    if ($result_P->num_rows > 0) {
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            if (in_array($rows['category_id'], $catarr)) {
                $html.=' <li><input type="checkbox" checked="true" name="categories[]" value="' . $rows['category_id'] . '"/>&nbsp;' . $rows['name'] . '</li>';
                $html.= get_sub_category_foredit_new($catarr, $rows['category_id'], '&nbsp;&nbsp;');
            } else {
                $html.=' <li><input type="checkbox" name="categories[]" value="' . $rows['category_id'] . '"/>&nbsp;' . $rows['name'] . '</li>';
                $html.= get_sub_category_foredit_new($catarr, $rows['category_id'], '&nbsp;&nbsp;');
            }
        }
        $result_P->free();
        return $html;
    } else {
        $result_P->free();
        return null;
    }
}

/*
 * Get subcategory
 */

function get_sub_category_foredit_new($catarr, $cat_id, $str) {
    global $az_db;
    $sql = "SELECT  `category_id` ,`name` , `category_parent_id` FROM  `az_category` WHERE category_parent_id = " . $cat_id;
    $result_P = $az_db->executeSQL_Query($sql);
    if ($result_P->num_rows > 0) {
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            if (in_array($rows['category_id'], $catarr)) {
                $html.='<li><input type="checkbox" checked="true" name="categories[]" value="' . $rows['category_id'] . '"/>&nbsp;' . $str . $rows['name'] . '</li>';
                $html.=get_sub_category_foredit_new($catarr, $rows['category_id'], '&nbsp;&nbsp;&nbsp;&nbsp;');
            } else {
                $html.='<li><input type="checkbox" name="categories[]" value="' . $rows['category_id'] . '"/>&nbsp;' . $str . $rows['name'] . '</li>';
                $html.=get_sub_category_foredit_new($catarr, $rows['category_id'], '&nbsp;&nbsp;&nbsp;&nbsp;');
            }
        }
        $result_P->free();
        return $html;
    } else {
        $result_P->free();
        return null;
    }
}

/*
 * Get edit post new
 */

function get_edit_post_template($params) {
    if (count($params) > 0 && is_array($params)) {
        global $tpl, $az_db;
        $file_tpl = 'news/frm_edit_new';
        $frmeditnew = $tpl->get_main($file_tpl, 2);
        $sql = "SELECT * FROM az_news WHERE `news_id`=" . $params['newId'];
        $result_P = $az_db->executeSQL_Query($sql);
        if ($result_P->num_rows > 0) {
            while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
                $newId = $rows['news_id'];
                $title = $rows['title'];
                $author = $rows['author'];
                $descr = $rows['description'];
                $content = $rows['content'];
                $category = $rows['category_id'];
                $active = $rows['status'];
                $tags = $rows['tags'];
                $image = $rows['image'];
                $survey_id = $rows['survey_id'];
                $comment_stt = $rows['comment_status'];
                $hot = $rows['hot'];

                //Block public
                $block_public = '';
                if ($active == 1) {
                    $block_public = '<li><input type="checkbox" name="checksttnew[]" value="savedraff"/>&nbsp;Save draff</li>
                                     <li><input type="checkbox" name="checksttnew[]" value="public" checked="true"/>&nbsp;Public</li>';
                } else {
                    $block_public = '<li><input type="checkbox" name="checksttnew[]" value="savedraff" checked="true"/>&nbsp;Save draff</li>
                                     <li><input type="checkbox" name="checksttnew[]" value="public"/>&nbsp;Public</li>';
                }
                if ($hot == 1) {
                    $block_public.= '<li><input type="checkbox" name="checksttnew[]" value="hot" checked="true"/>&nbsp;Hot</li>';
                } else {
                    $block_public.= '<li><input type="checkbox" name="checksttnew[]" value="hot"/>&nbsp;Hot</li>';
                }
                if ($comment_stt == 1) {
                    $block_public.= '<li><input type="checkbox" name="checksttnew[]" value="allowcom" checked="true"/>&nbsp;Allow comment</li>';
                } else {
                    $block_public.= '<li><input type="checkbox" name="checksttnew[]" value="allowcom" />&nbsp;Allow comment</li>';
                }
                //Block image
                $block_image = '<img src="' . $image . '" alt="Featured Image" width="130px" height="130px" />';
                //Block category 
                $block_category = get_list_category_image($category);
                $html = $tpl->get_vars($frmeditnew, array('action' => 'action.php?act=2', '_title' => $title, '_author' => $author,
                            'new_des' => $descr, 'new_tags' => $tags, 'new_content' => $content, 'image_links' => $image,
                            'block_new_public' => $block_public, 'block_new_image' => $block_image,
                            'edit_new_list_categories' => $block_category, 'new_id' => $newId));
            }
            $result_P->free();
            return $html;
        } else {
            $result_P->free();
            return null;
        }
    }
}

/*
 * Function get view content post new
 */

function get_view_content_post($params) {
    if (count($params) > 0 && is_array($params)) {
        global $tpl, $az_db;
        $file_tpl = 'news/view_content';
        $frmnew = $tpl->get_main($file_tpl, 2);
        $sql = "SELECT * FROM az_news WHERE `news_id`=" . $params['newId'];
        $result_P = $az_db->executeSQL_Query($sql);
        if ($result_P->num_rows > 0) {
            while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
                $newId = $rows['news_id'];
                $title = $rows['title'];
                $author = $rows['author'];
                $descr = $rows['description'];
                $content = $rows['content'];
                $category = $rows['category_id'];
                $active = $rows['status'];
                $tags = $rows['tags'];
                $image = $rows['image'];
                $survey_id = $rows['survey_id'];
                $comment_stt = $rows['comment_status'];
//                if ($image != '' && isset($image)) {
//                    $img_des = '<img src="' . $image . '" width="130px" height="130px" />';
//                } else {
//                    $img_des = '';
//                }
                $img_des = '';
                $actions = '<a href="actionform.php " title="Add" class="icon-add info-tooltip"></a>
                        <a href="edit.php?newId=' . $newId . ' " title="Edit" class="icon-edit info-tooltip"></a>
                        <a href="action.php?act=3&newId=' . $newId . ' " title="Delete" class="icon-delete info-tooltip" onclick="return deletechecked();"></a>
                        <a href="action.php?act=4&newId=' . $newId . ' " title="Active" class="icon-active info-tooltip"></a>
                        <a href="view.php?newId=' . $newId . ' " title="View" class="icon-view info-tooltip"></a>
                        <a href="action.php?act=6&newId=' . $newId . ' " title="Set hot" class="icon-hot info-tooltip"></a>';
                $html = $tpl->get_vars($frmnew, array('title_new' => $title, 'image_new' => $img_des, 'des_new' => $descr, 'content_new' => $content,
                            'author_new' => $author, 'action_new' => $actions));
            }
            $result_P->free();
            return $html;
        } else {
            $result_P->free();
            return '';
        }
    }
}

/*
 * Funciton get list new by category
 */

function get_list_new_by_category($params) {
    global $az_db, $tpl;
    $file_tpl = 'news/list_new_by_category';
    $list_new = $tpl->get_main($file_tpl, 2);

    $sql = "SELECT COUNT(*) FROM az_news WHERE `category_id`=" . $params['catId'];
    $result = $az_db->executeSQL_Query($sql)->fetch_row();
    $totalRecord = $result[0];
    if ($totalRecord > 0) {
        $pager = new Az_Pager();
        //Create params for pager
        $scroll_page = 5;
        $per_page = 6;
        $current_page = isset($_GET['page']) ? $_GET['page'] : 1;
        $url = 'litscatnew.php?page=';

        //Setup a page
        $pager->page_set($url, $totalRecord, $scroll_page, $per_page, $current_page);

        $sql = "SELECT * FROM az_news WHERE `category_id`=" . $params['catId'] . ' ORDER BY news_id DESC LIMIT ' . $pager->start . ',' . $pager->per_page;
        $result_P = $az_db->executeSQL_Query($sql);
        if ($result_P->num_rows > 0) {
            while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
                $newId = $rows['news_id'];
                $title = $rows['title'];
                $author = $rows['author'];
                $descr = $rows['description'];
                if ($image != '' && isset($image)) {
                    $img_des = '<img src="' . $image . '" width="130px" height="130px" />';
                } else {
                    $img_des = '';
                }
                //Actions form
                $actions = '<a href="actionform.php " title="Add" class="icon-add info-tooltip"></a>
                        <a href="edit.php?newId=' . $newId . ' " title="Edit" class="icon-edit info-tooltip"></a>
                        <a href="action.php?act=3&newId=' . $newId . ' " title="Delete" class="icon-delete info-tooltip" onclick="return deletechecked();"></a>
                        <a href="action.php?act=4&newId=' . $newId . ' " title="Active" class="icon-active info-tooltip"></a>
                        <a href="view.php?newId=' . $newId . ' " title="View" class="icon-view info-tooltip"></a>
                        <a href="action.php?act=6&newId=' . $newId . ' " title="Set hot" class="icon-hot info-tooltip"></a>';
                $listnew .=' <div class="list_content_wrapper">';
                if ($img_des == '') {
                    $listnew .='<div class="content-for-new"><h3>' . $title . '</h3><p>' . $descr . '</p>
                        <p class="action-new">' . $actions . '</p></div></div>';
                } else {
                    $listnew .='<div class="image-for-head"><img src="' . $img_des . '" width="130px" height="130px"/></div>' .
                            '<div class="content-for-new-img"><h3>' . $title . '</h3><p>' . $descr . '</p>
                        <p class="action-new">' . $actions . '</p></div></div>';
                }
            }
            $result_P->free();
            $html = $tpl->get_vars($list_new, array('categoryName' => $params['name'], 'list_new_by_cat' => $listnew,
                        'page_par_left' => $pager->firt_page, 'page_left' => $pager->previous_page,
                        'page-info' => $pager->page_info, 'page-right' => $pager->next_page, 'page-far-right' => $pager->last_page));
            return $html;
        } else {
            $result_P->free();
            return '';
        }
    }
}

?>
